﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Mail;
    using DotNetNuke.Services.Scheduling;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web.UI.WebControls;

    public class SchedulerReport : SchedulerClient
    {
        public SchedulerReport(ScheduleHistoryItem objScheduleHistoryItem)
        {
            this.ScheduleHistoryItem = objScheduleHistoryItem;
        }

        private DateTime calcNextRunTime(NB_Store_SQLReportInfo objRinfo)
        {
            DateTime time2 = DateTime.Now;
            if (Conversions.ToDouble(objRinfo.SchReRunMins) >= 1440.0)
            {
                objRinfo.LastRunTime = Conversions.ToDate(objRinfo.LastRunTime.ToShortDateString() + " " + objRinfo.SchStartHour + ":" + objRinfo.SchStartMins + ":00");
            }
            if (Versioned.IsNumeric(objRinfo.SchReRunMins))
            {
                time2 = DateAndTime.DateAdd(DateInterval.Minute, (double) Conversions.ToInteger(objRinfo.SchReRunMins), objRinfo.LastRunTime);
            }
            return time2;
        }

        private void DoReport()
        {
            IEnumerator enumerator=null;
            PortalController controller = new PortalController();
            SQLReportController controller2 = new SQLReportController();
            ArrayList allPortals = new SettingsController().GetAllPortals();
            try
            {
                enumerator = allPortals.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    PortalInfo current = (PortalInfo) enumerator.Current;
                    if (SharedFunctions.GetStoreSetting(current.PortalID, "version", "None") != "")
                    {
                        IEnumerator enumerator2=null;
                        ArrayList list2 = controller2.GetSQLAdminReportList(current.PortalID, true, "");
                        try
                        {
                            enumerator2 = list2.GetEnumerator();
                            while (enumerator2.MoveNext())
                            {
                                NB_Store_SQLReportInfo objRinfo = (NB_Store_SQLReportInfo) enumerator2.Current;
                                if (objRinfo.SchedulerFlag && (DateTime.Compare(DateAndTime.Now, this.calcNextRunTime(objRinfo)) > 0))
                                {
                                    this.RunReport(current, objRinfo);
                                    objRinfo.LastRunTime = DateAndTime.Now;
                                    controller2.UpdateObjSQLReport(objRinfo);
                                }
                            }
                            continue;
                        }
                        finally
                        {
                            if (enumerator2 is IDisposable)
                            {
                                (enumerator2 as IDisposable).Dispose();
                            }
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public override void DoWork()
        {
            try
            {
                this.Progressing();
                this.DoReport();
                this.ScheduleHistoryItem.Succeeded = true;
                this.ScheduleHistoryItem.AddLogNote("Store Reports Completed.");
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception objException = exception1;
                this.ScheduleHistoryItem.Succeeded = false;
                this.ScheduleHistoryItem.AddLogNote("Store Reports failed." + objException.ToString());
                this.ScheduleHistoryItem.Succeeded = false;
                this.Errored(ref objException);
                DotNetNuke.Services.Exceptions.Exceptions.LogException(objException);
                ProjectData.ClearProjectError();
            }
        }

        private void RunReport(PortalInfo objPInfo, NB_Store_SQLReportInfo objRinfo)
        {
            SQLReportController controller = new SQLReportController();
            if (objRinfo != null)
            {
                objRinfo.SQL = controller.insertParams(objRinfo.ReportID, -1, objPInfo.PortalID, null, objRinfo.SQL, SharedFunctions.GetCurrentCulture());
                DataGrid grid = new DataGrid();
                string dataHTML = "";
                dataHTML = controller.GetReportOutput(objRinfo);
                if (((objRinfo.EmailResults & (dataHTML != "")) & (objRinfo.EmailFrom != "")) & (objRinfo.EmailTo != ""))
                {
                    dataHTML = controller.AddStoreCSSforEmailHTML(dataHTML);
                    dataHTML = objRinfo.ReportTitle + "<br/>" + dataHTML;
                    DotNetNuke.Services.Mail.Mail.SendMail(objRinfo.EmailFrom, objRinfo.EmailTo, "", objRinfo.ReportName, dataHTML, "", "HTML", "", "", "", "");
                }
            }
        }
    }
}

